{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c9a180c2",
   "metadata": {},
   "source": [
    "# Week06_note\n",
    "A. 列表的基本方法\n",
    "\n",
    "    a. 列表的私有方法\n",
    "    b. 列表的其他方法\n",
    "\n",
    "B. 列表的高级方法\n",
    "\n",
    "    a. 复制\n",
    "    b. 枚举\n",
    "    \n",
    "## 1.列表的基本方法\n",
    "### 1.1 列表的私有方法\n",
    "#### 1.1.1 names.\n",
    "A.在列表中增加的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b84e0ef3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Mike', 'Mary', 'Jan', 'Jack', 'Alex']\n"
     ]
    }
   ],
   "source": [
    "# a. append=增加（增加在list的最后一位）\n",
    "names = [\"Mike\",\"Mary\",\"Jan\",\"Jack\"]\n",
    "names.append(\"Alex\")\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4a11492b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "insert之后的names: ['youge', 'Mike', 'Mary', 'Jan', 'Jack', 'Alex']\n",
      "insert两次之后的names: ['youge', 'Mike', 'youge', 'Mary', 'Jan', 'Jack', 'Alex']\n"
     ]
    }
   ],
   "source": [
    "# b. insert=插入（添加索引，可以指定位置增加元素）\n",
    "names.insert(0,\"youge\")\n",
    "print(\"insert之后的names:\",names)\n",
    "names.insert(2,\"youge\")\n",
    "print(\"insert两次之后的names:\",names)\n",
    "\n",
    "# 列表可以出现重复的值 如果要放唯一值，列表不是最优选"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96c6f0a4",
   "metadata": {},
   "source": [
    "B. 在列表中删除的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e525ad80",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remove删除后的结果: ['Mike', 'Mary', 'Jan', 'Jack']\n"
     ]
    }
   ],
   "source": [
    "# a. remove() （一般情况下使用，先用 in 判断）\n",
    "names = ['Mike', 'Mary', 'Jan', 'Jack', 'Alex']\n",
    "if \"Alex\" in names:\n",
    "    names.remove(\"Alex\")\n",
    "    print(\"remove删除后的结果:\",names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "bb6f8481",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pop()方法后的： ['Mike', 'Jan', 'Jack', 'Alex']\n"
     ]
    }
   ],
   "source": [
    "# b. pop()    （pop()删除位置为1的这个元素）\n",
    "names = ['Mike', 'Mary', 'Jan', 'Jack', 'Alex']\n",
    "names.pop(1)\n",
    "print(\"pop()方法后的：\",names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "84a7e172",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pop()方法后的： ['Mike', 'Jan', 'Jack', 'Alex']\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "list.remove(x): x not in list",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-13-7599836bbcd4>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mnames\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"pop()方法后的：\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mnames\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0mnames\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Mary\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      6\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnames\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: list.remove(x): x not in list"
     ]
    }
   ],
   "source": [
    "## 删除和修改无须验证，否则会报错\n",
    "names = ['Mike', 'Mary', 'Jan', 'Jack', 'Alex']\n",
    "names.pop(1)\n",
    "print(\"pop()方法后的：\",names)\n",
    "names.remove(\"Mary\")\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cbdc60fa",
   "metadata": {},
   "source": [
    "C. index()索引 查看该元素如\"1001\"在stu_id中第一个出现的位置索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "077f8cba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "stu_id = [1001,1002,1003,1004,1001,1008,1009,1010,1001]\n",
    "print(stu_id.index(1001))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e486dcd",
   "metadata": {},
   "source": [
    "D. clear()清除所有数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "087f1383",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "清除列表后的值： []\n"
     ]
    }
   ],
   "source": [
    "names = ['Mike', 'Mary', 'Jan', 'Jack', 'Alex']\n",
    "names.clear()\n",
    "print(\"清除列表后的值：\",names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ff05b0c",
   "metadata": {},
   "source": [
    "E. 查看列表元素出现的个数,（简单应用）统计分数出现的次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "bfdd5be9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "count方法后的结果 3\n"
     ]
    }
   ],
   "source": [
    "stu_id = [1001,1002,1003,1004,1001,1008,1009,1010,1001]\n",
    "print(\"count方法后的结果\",stu_id.count(1001))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb53d94a",
   "metadata": {},
   "source": [
    "F. extend （）是给指定的list进行扩充"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "01b82cff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Mike', 'Mary', 'Jan', 'Jack', 'Alex', 1001, 1002, 1003, 1004, 1001, 1008, 1009, 1010, 1001]\n"
     ]
    }
   ],
   "source": [
    "names = ['Mike', 'Mary', 'Jan', 'Jack', 'Alex']\n",
    "stu_id = [1001,1002,1003,1004,1001,1008,1009,1010,1001]\n",
    "names.extend(stu_id)\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f95b8134",
   "metadata": {},
   "source": [
    "# 1.2列表的其他方法（Sequence Types 的一些方法）\n",
    "### 1.2.1 len()查看list长度\n",
    "### print(\"names的长度/names的列表元素个数:\",len(names))\n",
    "### 1.2.2 in查看元素是否在list当中，结果为布尔值， False/True,\n",
    "### 一般在操作list的修改和删除时会先判断\n",
    "### 判断字段是否在数据里，用“in” 再判断是否要删除或修改\n",
    "#### \"Alex\" in name\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "a90d0a90",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 例如：\n",
    "letters = ['a','b','c','d']\n",
    "len(letters)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "84b60c78",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names = ['Mike', 'Mary', 'Jan', 'Jack', 'Alex']\n",
    "len(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7268ac5b",
   "metadata": {},
   "source": [
    "# 2. 列表的高级方法\n",
    "## 2.1 复制\n",
    "### e.g：stu_id = [1001,1002,1003,1004,1001,1008,1009,1010,1001]\n",
    "#### **copy()**\n",
    "    思考：赋值是真正的复制吗？ 赋值（=）<=> copy ? 不等价\n",
    "    stu_id_matedata = stu_id\n",
    "    print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)\n",
    "#### 根据以下实验得出结论，赋值不是真正的复制，copy()和切片可以实现复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "288f2569",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stu_id: [1001, 1002, 1003, 1004, 1001, 1008, 1009, 1010, 1001] \n",
      " stu_id_matedata: [1001, 1002, 1003, 1004, 1001, 1008, 1009, 1010, 1001]\n",
      "stu_id: [1001, 1003, 1004, 1001, 1008, 1009, 1010, 1001] \n",
      " stu_id_matedata: [1001, 1003, 1004, 1001, 1008, 1009, 1010, 1001]\n"
     ]
    }
   ],
   "source": [
    "# 实验1——赋值\n",
    "stu_id = [1001,1002,1003,1004,1001,1008,1009,1010,1001]\n",
    "stu_id_matedata = stu_id\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)\n",
    "stu_id.pop(1)\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)\n",
    "# 结论：无法实现真正的复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "c97a6ea8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stu_id: [1001, 1002, 1003, 1004, 1001, 1008, 1009, 1010, 1001] \n",
      " stu_id_matedata: [1001, 1002, 1003, 1004, 1001, 1008, 1009, 1010, 1001]\n",
      "stu_id: [1001, 1003, 1004, 1001, 1008, 1009, 1010, 1001] \n",
      " stu_id_matedata: [1001, 1002, 1003, 1004, 1001, 1008, 1009, 1010, 1001]\n"
     ]
    }
   ],
   "source": [
    "# 实验2——copy()\n",
    "stu_id = [1001,1002,1003,1004,1001,1008,1009,1010,1001]\n",
    "stu_id_matedata = stu_id\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)\n",
    "stu_id_matedata = stu_id.copy()\n",
    "stu_id.pop(1)\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "ebde5a2f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stu_id: [1001, 1002, 1003, 1004, 1001, 1008, 1009, 1010, 1001] \n",
      " stu_id_matedata: [1001, 1002, 1003, 1004, 1001, 1008, 1009, 1010, 1001]\n",
      "stu_id: [1001, 1003, 1004, 1001, 1008, 1009, 1010, 1001] \n",
      " stu_id_matedata: [1001, 1002, 1003, 1004, 1001, 1008, 1009, 1010, 1001]\n"
     ]
    }
   ],
   "source": [
    "# 实验3——切片实现复制\n",
    "stu_id = [1001,1002,1003,1004,1001,1008,1009,1010,1001]\n",
    "stu_id_matedata = stu_id\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)\n",
    "stu_id_matedata = stu_id[:]\n",
    "stu_id.pop(1)\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae2e44c8",
   "metadata": {},
   "source": [
    "## 2.2 枚举\n",
    "stu_id = [1001,1002,1003,1004,1001,1008,1009,1010,1001]\n",
    "### 思考：如何取出所有位置1001的index？\n",
    "#### 错误的方法\n",
    "    for i in stu_id:\n",
    "    # 循环遍历stu_id的所有内容\n",
    "    if i == 1001:\n",
    "        # 判断如果元素为1001时，怎么取出索引值呢？\n",
    "        # 错误方法：print(stu_id.index(i))\n",
    "        print(stu_id.index(i))\n",
    "        print()\n",
    "#### 该方法只能取出第一个1001的index\n",
    "\n",
    "### 正确的方法——枚举\n",
    "    枚举：因为list其实不仅有values值，还有index索引，但for循环主要循环遍历其值，不遍历所有\n",
    "    枚举可以同时遍历两者\n",
    "#### enumerate() 将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列，同时列出数据和数据下标，一般用在 for 循环当中。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "6b64728c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1001\n",
      "3 1001\n",
      "7 1001\n",
      "我们想要删除pop()的值 [0, 3, 7]\n"
     ]
    }
   ],
   "source": [
    "pop_values_list = [] # append方法经常用于新建列表\n",
    "for index,item in enumerate(stu_id):\n",
    "    # 第一个print，print出\"stu_id\"的所有值和index\n",
    "    # print(index,item)\n",
    "    if item == 1001:\n",
    "        # 第二个print，print出1001的index和值\n",
    "        print(index,item)\n",
    "        pop_values_list.append(index)\n",
    "print(\"我们想要删除pop()的值\",pop_values_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa3ae446",
   "metadata": {},
   "source": [
    "# 3.练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "4ba3fbf3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "DBe happy!oBe happy!nBe happy!'Be happy!tBe happy! Be happy!pBe happy!aBe happy!nBe happy!iBe happy!cBe happy!!\n"
     ]
    }
   ],
   "source": [
    "# 3.1 P67\n",
    "phrase = \"Don't panic!\"\n",
    "# 把字符串转变为list\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)\n",
    "# 将列表转化为字符串\n",
    "new_phrase = \"Be happy!\".join(plist)\n",
    "print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "878a320b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "pop结果后的值: ['o', 'n', \"'\", 't', ' ', 'p', 'a']\n",
      "remove结果后的值: ['o', 'n', 't', ' ', 'p', 'a']\n",
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "转化为字符串后： on tap\n"
     ]
    }
   ],
   "source": [
    "# 将字符串\"Don't panic!\" 转换为新的字符串\"on tap\"\n",
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)\n",
    "# 对plist进行四次循环\n",
    "for i in range(4):\n",
    "    # 利用pop()对4次循环末尾的值进行删除——\"nic!\"\n",
    "    plist.pop()\n",
    "# 利用pop()删除第0位的值    \n",
    "plist.pop(0)\n",
    "print(\"pop结果后的值:\",plist)\n",
    "# 利用remove()将列表中的“'”删除\n",
    "plist.remove(\"'\")\n",
    "print(\"remove结果后的值:\",plist)\n",
    "# 交换列表末尾的两个对象——先将末尾的两个值提出，再进行扩展\n",
    "plist.extend([plist.pop(),plist.pop()])\n",
    "# **空格也是字符**\n",
    "# 将空格字符提出，利用insert插入在第二位\n",
    "plist.insert(2,plist.pop(3))\n",
    "print(plist)\n",
    "# 将列表转化为字符串\n",
    "new_phrase = ''.join(plist)\n",
    "print(plist)\n",
    "print(\"转化为字符串后：\",new_phrase)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a2021ef",
   "metadata": {},
   "source": [
    "## 补充： 课堂跟随练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "45709044",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['o', 'n']\n",
      "on\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)  # list()可以将 str to(转换成) list\n",
    "print(phrase)\n",
    "print(plist)\n",
    "# # 删除后面三位元素\n",
    "# for i in range(4):\n",
    "#     plist.pop()\n",
    "# print(plist)\n",
    "#\n",
    "# # 删除 D ‘\n",
    "# plist.pop(0)\n",
    "# # 或 plist.remove(\"D\")\n",
    "# plist.remove(\"'\")\n",
    "# # 或 plist.pop(2)\n",
    "# print(plist)\n",
    "#\n",
    "# # plist.extend()\n",
    "# # 数学案例：计算（（a+b）*（c+d））\n",
    "# plist.extend([plist.pop(),plist.pop()])\n",
    "# #['o', 'n', 't', ' '] [a , p ]\n",
    "# print(plist)\n",
    "#\n",
    "# # insert\n",
    "# # plist.insert(2,plist.pop(3))\n",
    "# # print(plist)\n",
    "# # print(plist.pop(2)) # 1.代表删除的内容的值 2.他删除了plist这个值\n",
    "# plist.insert(3,plist.pop(2))\n",
    "# print(plist)\n",
    "#\n",
    "# # list to str (join之前的’‘代表以什么将字符串合并)\n",
    "# new_phrase = ''.join(plist)\n",
    "# print(plist)\n",
    "# print(new_phrase)\n",
    "\n",
    "# plist 改变了原先的状态吗？列表的方法会改变列表本身，如果不想打破或者想保留原先数据的内容，请用copy()复制\n",
    "\n",
    "# 方法二：切片方法实现\n",
    "print(plist[1:3])\n",
    "on_str = ''.join(plist[1:3])\n",
    "print(on_str)\n",
    "new_phrase = on_str + ''.join(plist[5]+plist[4]+plist[7]+plist[6])\n",
    "print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "134fb42e",
   "metadata": {},
   "source": [
    "## 4. 补充：利用remove和pop()删除值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "a1ea52ed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1002, 1003, 1004, 1001, 1008, 1009, 1010, 1001]\n",
      "[1002, 1003, 1004, 1008, 1009, 1010, 1001]\n",
      "[1002, 1003, 1004, 1008, 1009, 1010]\n"
     ]
    }
   ],
   "source": [
    "# 4.1 remove\n",
    "stu_id = [1001,1002,1003,1004,1001,1008,1009,1010,1001]\n",
    "while True:\n",
    "    if 1001 in stu_id:\n",
    "        stu_id.remove(1001)\n",
    "        print(stu_id)\n",
    "    else:\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "2f411a9b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1001的索引值 0\n",
      "1001的索引值 3\n",
      "1001的索引值 6\n",
      "删除1001后的stu_id： [1002, 1003, 1004, 1008, 1009, 1010]\n"
     ]
    }
   ],
   "source": [
    "# 4.2 pop()\n",
    "stu_id = [1001,1002,1003,1004,1001,1008,1009,1010,1001]\n",
    "# print(stu_id.index(1001))\n",
    "for index,item in enumerate(stu_id):\n",
    "#     print(index,item)\n",
    "    if item == 1001:\n",
    "        print(\"1001的索引值\",index)\n",
    "        stu_id.pop(index)\n",
    "print(\"删除1001后的stu_id：\",stu_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dfea4ebe",
   "metadata": {},
   "source": [
    "## 5.课本中的切片练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "2878fef0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "on\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "取出相应字母的新字符串： on tap\n"
     ]
    }
   ],
   "source": [
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)\n",
    "# 从plist中取出\"on\",plist[1:3]即从第一位置开始取三位，索引值为3的取不到\n",
    "new_phrase = ''.join(plist[1:3])\n",
    "print(new_phrase)\n",
    "# 继续取出所需要的字母\n",
    "new_phrase = new_phrase + ''.join([plist[5],plist[4],plist[7],plist[6]])\n",
    "print(plist)\n",
    "print(\"取出相应字母的新字符串：\",new_phrase)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11b24c3b",
   "metadata": {},
   "source": [
    "# 总结\n",
    "## 1.list方法的总结\n",
    "\n",
    "| names.  \t| description                                \t| how to use                                \t|\n",
    "|---------\t|--------------------------------------------\t|-------------------------------------------\t|\n",
    "| append  \t| 增加在list的末尾                           \t| names.append(\"Alex\")                      \t|\n",
    "| insert  \t| 添加索引，可以指定位置增加元素             \t| names.insert(2,\"youge\")                   \t|\n",
    "| remove  \t| 删除元素，一般先用\"in\"判断                 \t| if \"Alex\" in names:  names.remove(\"Alex\") \t|\n",
    "| pop     \t| 删除指定位置的元素，()无内容则删除末尾元素 \t| names.pop(1)                              \t|\n",
    "| index() \t| 查看某元素在list中第一个出现的位置索引     \t| print(names.index(1001))                  \t|\n",
    "| clear() \t| 清除所有数据                               \t| names.clear()                             \t|\n",
    "| count() \t| 查看list中元素的个数                       \t| print(names.count(1001))                  \t|\n",
    "| extend  \t| 给指定list进行扩充                         \t| names.extend(stu_id)                      \t|\n",
    "\n",
    "## 2. list切片的总结\n",
    "### e.g: data_list=[12,23,34,45,56]\n",
    "\n",
    "| slice \t| description                     \t| how to use                          \t|\n",
    "|-------\t|------------------------------------   \t |-------------------------------------\t         |\n",
    "| [2]     | 从左往右取位置为2的值                | print(data_list[2])——34            \t   |\n",
    "| [0:3] \t| 从左往右取出位置0到2的值，最右边不取      | print(data_list[0:3])——12,23,34     \t     |\n",
    "| [:3]  \t| 若要从0开始计算，0可以不写          \t  | print(data_list[:3])——12,23,34     \t       |\n",
    "| [-1]  \t| 从右往左取位置为1的值                | print(data_list[-1])——56          \t     |\n",
    "| [-3:] \t| 若要取最后一个值，不能写0，直接省略 \t    | print(data_list[-3:])——34,45,56     \t      |\n",
    "| [:]     | 取所有的值                       | print(data_list[:])——12,23,34,45,56 \t       |"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
